   //
//  SYDMyWalletViewController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/8.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDMyWalletViewController.h"
#import "SYDwithdrawView.h"
@interface SYDMyWalletViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *moneyTopConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UIView *moneyView;
@property (weak, nonatomic) IBOutlet UILabel *moneyLbl;
@property (weak, nonatomic) IBOutlet UIView *withdraw;
@property (weak, nonatomic) IBOutlet UIButton *withdrawBtn;
@property(nonatomic,strong)MyWalletModel *model;

@property (nonatomic, strong) UIView *maskView;//遮罩View
@property (nonatomic, strong) SYDwithdrawView *withdrawView;

@end

@implementation SYDMyWalletViewController
#pragma mark - 导航栏
- (void)navigationBarHandle{
    
    WEAKSELF
    [weakSelf backButtonAction:^{
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    [weakSelf rightNavButtonWithTitle:@"钱包明细" action:^{
        [self.navigationController pushViewController:[[SYDWalletDetailViewController alloc]init] animated:YES];
    }];
}
-(void)getMyWalletData{
    WEAKSELF   //@"0c376d207beb11e797f200e066d54d33"
    [[SYDNetwork shareTool]getMineWalletWithUserId:[YMSaveTool valueForKey:USER_ID] success:^(NSURLSessionDataTask *task, id responseObject) {
      @try {
          if ([responseObject[@"statusCode"]integerValue] == 200) {
            NSLog(@"----%@",responseObject);
              if ([responseObject[@"obj"] isKindOfClass:[NSDictionary class]]) {
                  MyWalletModel *model = [MyWalletModel mj_objectWithKeyValues:responseObject[@"obj"]];
                  weakSelf.model = model;
//                  weakSelf.moneyLbl.text = [model.balance stringByAppendingString:@"元"];
                  weakSelf.moneyLbl.text = [NSString stringWithFormat:@"%.2f",[model.balance floatValue]];
              } else {
                  weakSelf.moneyLbl.text = @"0.00";
                  weakSelf.withdrawBtn.userInteractionEnabled = NO;
                  weakSelf.withdrawBtn.backgroundColor = GRAY_COLOR;
              }
          }else{
              [SVProgressHUD displayInfoWithStatus:responseObject[@"msg"]];
            }
      } @catch (NSException *exception) {
          NSLog(@"---%@",exception.description);
      } @finally {
      }
  } failure:^(NSURLSessionDataTask *task, NSError *error) {
      NSLog(@"%@",error);
  }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (iPhone8_X) {
        self.topConstraint.constant = 88;
        self.moneyTopConstraint.constant  = 108;
    } else {
        self.topConstraint.constant = 64;
        self.moneyTopConstraint.constant  = 96;
    }
    [self getMyWalletData];
}
-(void)viewDidLoad {
    [super viewDidLoad];
    
    self.bottomView.backgroundColor = GREEN_COLOR;
    self.navigationBar.title = @"我的钱包";
    [self navigationBarHandle];

    [self initMyWalletUI];
    self.withdrawBtn.eventTimeInterval = 3.0f;
    WEAKSELF
    [[self.withdrawBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if ([weakSelf.model.isFrozen isEqualToString:@"1"]) {
            self.withdrawBtn.enabled = YES;
            self.withdrawBtn.backgroundColor = GRAY_COLOR;
            [SVProgressHUD displayInfoWithStatus:@"用户被冻结" time:1.5f];
        }else{
            SYDProduceProgressViewController *progressVc = [[SYDProduceProgressViewController alloc]init];
            progressVc.walletModel = weakSelf.model;
            [weakSelf.navigationController pushViewController:progressVc animated:YES];
        }
    }];
}
-(void)initMyWalletUI{
    
    self.withdrawBtn.eventTimeInterval = 3.0;
    [self.moneyView drawBorderWithCornerRadius:10];
    [self.withdrawBtn drawBorderWithCornerRadius:5];
    [self.withdraw jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [self.view addSubview:self.maskView];
        [self.view addSubview:self.withdrawView];
    }];
    WEAKSELF
    // 点击遮罩层
    [self.maskView jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [weakSelf removeMaskOrPopup];
    }];
    self.withdrawView.block = ^{
        [weakSelf removeMaskOrPopup];
    };
}
// 移除遮罩层和弹窗
- (void)removeMaskOrPopup{
    
    [self.withdrawView removeFromSuperview];
    
    [self.maskView removeFromSuperview];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- (UIView *)maskView{
    if (!_maskView) {
        _maskView = [[UIView alloc] initWithFrame:SCREEN_RECT];
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.alpha = 0.3;
    }
    return _maskView;
}
-(SYDwithdrawView *)withdrawView{
    if (!_withdrawView) {
        _withdrawView = [[SYDwithdrawView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2 - 135, 190, 270, 212)];
    }
    return _withdrawView;
}

-(MyWalletModel *)model{
    if (!_model) {
        _model = [[MyWalletModel alloc]init];
    }
    return _model;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
